using Microsoft.AspNetCore.Mvc;
using SoftDemo.Application.Common.Interface;
using SoftDemo.Infrastructrue.Utils;

namespace SoftDemo.Api.Controllers
{
    [Route("[controller]")]
    public class FileController : ControllerBase
    {
        private readonly IAppFileUploadServices _appFileUploadServices;

        public FileController(IAppFileUploadServices appFileUploadServices)
        {
            _appFileUploadServices = appFileUploadServices;
        }


        [HttpPost]
        public async Task<string> UploadFile(IFormCollection uploadFiles)
        {
            var list = await _appFileUploadServices.UploadFilesAsync(uploadFiles);

            if (list.Count() > 0)
            {
                var res = new
                {
                    Code = 1000,
                    Msg = "上传成功",
                    Data = list
                };

                return res.SerializeObject();
            }
            else
            {
                var res = new
                {
                    Code = 4000,
                    Msg = "上传失败，有不允许上传的文件扩展名或超出允许的文件大小",
                    Data = list
                };

                return res.SerializeObject();
            }
        }

        [HttpGet("{id}")]
        public async Task<FileContentResult> GetFile(Guid id)
        {
            var f = await _appFileUploadServices.GetFileByIdAsync(id);
            return f;
        }
    }
}